<?php

/**
 * This is the model class for table "{{links}}".
 *
 * The followings are the available columns in table '{{links}}':
 * @property integer $link_id
 * @property string $link_name
 * @property string $link_href
 * @property integer $link_type
 * @property integer $link_status
 * @property string $link_logo
 * @property integer $link_order
 * @property string $link_description
 */
class Links extends CActiveRecord
{
	const LINK_WORD = 1;
	const LINK_IMAGE = 2;
	
	const LINK_STATUS_ALLOW = 1;
	const LINK_STATUS_CANCEL = 3;
	const LINK_STATUS_TRASH = 2;
	
	public $link_order = 5;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Links the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{links}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('link_name, link_href, link_type', 'required'),
			array('link_type, link_order', 'numerical', 'integerOnly'=>true),
			array('link_name', 'length', 'max'=>200),
			array('link_href, link_logo', 'length', 'max'=>500),
			array('link_description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('link_id, link_name, link_href, link_type, link_logo, link_order, link_description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'link_id' => Yii::t('links','Link'),
			'link_name' => Yii::t('links','Link Name'),
			'link_href' => Yii::t('links','Link Href'),
			'link_type' => Yii::t('links','Link Type'),
			'link_status' => Yii::t('links','Link Status'),
			'link_logo' => Yii::t('links','Link Logo'),
			'link_order' => Yii::t('links','Link Order'),
			'link_description' => Yii::t('links','Link Description'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('link_id',$this->link_id);
		$criteria->compare('link_name',$this->link_name,true);
		$criteria->compare('link_href',$this->link_href,true);
		$criteria->compare('link_type',$this->link_type);
		$criteria->compare('link_logo',$this->link_logo,true);
		$criteria->compare('link_order',$this->link_order);
		$criteria->compare('link_description',$this->link_description,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getLinkType($type)
	{
		switch ($type)
		{
			case self::LINK_WORD:
				return Yii::t('links','Word Links');
				break;
			case self::LINK_IMAGE:
				return Yii::t('links','Image Links');
				break;
		}
	}
	
	public function generateLinkTypeDropDownList()
	{
		return array(
			self::LINK_WORD => self::getLinkType(self::LINK_WORD),
			self::LINK_IMAGE => self::getLinkType(self::LINK_IMAGE)
		);
	}
	
	public function getLinkStatus($status)
	{
		switch ($status)
		{
			case self::LINK_STATUS_CANCEL;
				return Yii::t('links','Private');
				break;
			case self::LINK_STATUS_ALLOW;
				return Yii::t('links','Public');
				break;
			case self::LINK_STATUS_TRASH;
				return Yii::t('links','Trash');
				break;
		}
	}
	
	public function generateLinkStatusDropDownList()
	{
		return array(
			self::LINK_STATUS_ALLOW => self::getLinkStatus(self::LINK_STATUS_ALLOW),
			self::LINK_STATUS_CANCEL => self::getLinkStatus(self::LINK_STATUS_CANCEL),
			self::LINK_STATUS_TRASH => self::getLinkStatus(self::LINK_STATUS_TRASH)
		);
	}
	
	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
	
				$this->link_status = self::LINK_STATUS_CANCEL;	
			}
			return true;
		}
		else
			return false;
	}
}